package com.haitaoit.chengquan.module.car.adapter;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.center.model.MyCouponModel2;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * Created by Administrator on 2016/12/24.
 */
public class CouponAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<MyCouponModel2> list;

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public CouponAdapter(Context context) {
        this.context = context;
    }

    public List<MyCouponModel2> getList() {
        return list;
    }

    public void setList(List<MyCouponModel2> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.my_coupon_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // 部分字体变大变色
        SpannableString msp = new SpannableString("¥" + list.get(position).getMoney());
        msp.setSpan(new RelativeSizeSpan(2f), 1, list.get(position).getMoney().length() + 1,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);// 大
        msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 1,
                list.get(position).getMoney().length() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);// 粗体
        msp.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.red)), 0,
                list.get(position).getMoney().length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.coupon_num.setText(msp);
        holder.coupon_name.setText(list.get(position).getName());
        holder.coupon_conditions.setText("· 消费满" + list.get(position).getConditions() + "元可用");
        holder.coupon_time.setText("· " + list.get(position).getSta_time() + " - " + list.get(position).getEnd_time());
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.coupon_num)
        TextView coupon_num;
        @ViewInject(R.id.coupon_name)
        TextView coupon_name;
        @ViewInject(R.id.coupon_conditions)
        TextView coupon_conditions;
        @ViewInject(R.id.coupon_time)
        TextView coupon_time;
    }
}
